﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Tab_RoleInfo
	/// </summary>
	public partial class Tab_RoleInfo:ITab_RoleInfo
	{
		public Tab_RoleInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("rid", "Tab_RoleInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int rid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Tab_RoleInfo");
			strSql.Append(" where rid=@rid");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)
			};
			parameters[0].Value = rid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}



        /// <summary>
        /// 修改角色的时候修改权限相关信息
        /// </summary>
        public bool Proc_RoleEdit(int r_optionAction, int r_leve, string r_powerRights, string r_rname, string r_rnote, int r_id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {	
		                 new SqlParameter("@r_optionAction", SqlDbType.Int), 
			 			 new SqlParameter("@r_leve", SqlDbType.VarChar,50), 
			 			 new SqlParameter("@r_powerRights", SqlDbType.VarChar,50), 
			 			 new SqlParameter("@r_rname", SqlDbType.VarChar,50), 
			 			 new SqlParameter("@r_rnote", SqlDbType.VarChar,50), 
			 			 new SqlParameter("@r_id", SqlDbType.VarChar,50) 
			 			 };
            parameters[0].Value = r_optionAction;
            parameters[1].Value = r_leve;
            parameters[2].Value = r_powerRights;
            parameters[3].Value = r_rname;
            parameters[4].Value = r_rnote;
            parameters[5].Value = r_id;
            DbHelperSQL.RunProcedure("Proc_RoleEdit", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ZY.Model.Tab_RoleInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Tab_RoleInfo(");
			strSql.Append("r_name,r_upid,r_leve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3)");
			strSql.Append(" values (");
			strSql.Append("@r_name,@r_upid,@r_leve,@r_createtime,@r_lasttime,@r_note,@r_ext1,@r_ext2,@r_ext3)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@r_name", SqlDbType.VarChar,50),
					new SqlParameter("@r_upid", SqlDbType.Int,4),
					new SqlParameter("@r_leve", SqlDbType.Int,4),
					new SqlParameter("@r_createtime", SqlDbType.DateTime),
					new SqlParameter("@r_lasttime", SqlDbType.DateTime),
					new SqlParameter("@r_note", SqlDbType.VarChar,5000),
					new SqlParameter("@r_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext3", SqlDbType.VarChar,50)};
			parameters[0].Value = model.r_name;
			parameters[1].Value = model.r_upid;
			parameters[2].Value = model.r_leve;
			parameters[3].Value = model.r_createtime;
			parameters[4].Value = model.r_lasttime;
			parameters[5].Value = model.r_note;
			parameters[6].Value = model.r_ext1;
			parameters[7].Value = model.r_ext2;
			parameters[8].Value = model.r_ext3;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZY.Model.Tab_RoleInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Tab_RoleInfo set ");
			strSql.Append("r_name=@r_name,");
			strSql.Append("r_upid=@r_upid,");
			strSql.Append("r_leve=@r_leve,");
			strSql.Append("r_createtime=@r_createtime,");
			strSql.Append("r_lasttime=@r_lasttime,");
			strSql.Append("r_note=@r_note,");
			strSql.Append("r_ext1=@r_ext1,");
			strSql.Append("r_ext2=@r_ext2,");
			strSql.Append("r_ext3=@r_ext3");
			strSql.Append(" where rid=@rid");
			SqlParameter[] parameters = {
					new SqlParameter("@r_name", SqlDbType.VarChar,50),
					new SqlParameter("@r_upid", SqlDbType.Int,4),
					new SqlParameter("@r_leve", SqlDbType.Int,4),
					new SqlParameter("@r_createtime", SqlDbType.DateTime),
					new SqlParameter("@r_lasttime", SqlDbType.DateTime),
					new SqlParameter("@r_note", SqlDbType.VarChar,5000),
					new SqlParameter("@r_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@rid", SqlDbType.Int,4)};
			parameters[0].Value = model.r_name;
			parameters[1].Value = model.r_upid;
			parameters[2].Value = model.r_leve;
			parameters[3].Value = model.r_createtime;
			parameters[4].Value = model.r_lasttime;
			parameters[5].Value = model.r_note;
			parameters[6].Value = model.r_ext1;
			parameters[7].Value = model.r_ext2;
			parameters[8].Value = model.r_ext3;
			parameters[9].Value = model.rid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int rid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_RoleInfo ");
			strSql.Append(" where rid=@rid");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)
			};
			parameters[0].Value = rid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ridlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_RoleInfo ");
			strSql.Append(" where rid in ("+ridlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_RoleInfo GetModel(int rid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 rid,r_name,r_upid,r_leve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 from Tab_RoleInfo ");
			strSql.Append(" where rid=@rid");
			SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)
			};
			parameters[0].Value = rid;

			ZY.Model.Tab_RoleInfo model=new ZY.Model.Tab_RoleInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_RoleInfo DataRowToModel(DataRow row)
		{
			ZY.Model.Tab_RoleInfo model=new ZY.Model.Tab_RoleInfo();
			if (row != null)
			{
				if(row["rid"]!=null && row["rid"].ToString()!="")
				{
					model.rid=int.Parse(row["rid"].ToString());
				}
				if(row["r_name"]!=null)
				{
					model.r_name=row["r_name"].ToString();
				}
				if(row["r_upid"]!=null && row["r_upid"].ToString()!="")
				{
					model.r_upid=int.Parse(row["r_upid"].ToString());
				}
				if(row["r_leve"]!=null && row["r_leve"].ToString()!="")
				{
					model.r_leve=int.Parse(row["r_leve"].ToString());
				}
				if(row["r_createtime"]!=null && row["r_createtime"].ToString()!="")
				{
					model.r_createtime=DateTime.Parse(row["r_createtime"].ToString());
				}
				if(row["r_lasttime"]!=null && row["r_lasttime"].ToString()!="")
				{
					model.r_lasttime=DateTime.Parse(row["r_lasttime"].ToString());
				}
				if(row["r_note"]!=null)
				{
					model.r_note=row["r_note"].ToString();
				}
				if(row["r_ext1"]!=null)
				{
					model.r_ext1=row["r_ext1"].ToString();
				}
				if(row["r_ext2"]!=null)
				{
					model.r_ext2=row["r_ext2"].ToString();
				}
				if(row["r_ext3"]!=null)
				{
					model.r_ext3=row["r_ext3"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select rid,r_name,r_upid,r_leve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 ");
			strSql.Append(" FROM Tab_RoleInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" rid,r_name,r_upid,r_leve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 ");
			strSql.Append(" FROM Tab_RoleInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Tab_RoleInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.rid desc");
			}
			strSql.Append(")AS Row, T.*  from Tab_RoleInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Tab_RoleInfo";
			parameters[1].Value = "rid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

